#ifndef SINGTON_H
#define SINGTON_H
#include <QMutex>
#include <QMutexLocker>

/*!
 * @ClassName : Sington
 * @Brief     : 单例模式的模板
 * @Author    : BinWatson
 * @Data      : 2022-05-27 10:52:14
 * @Details   : 详细使用可以参考TcpManager::instance()的实现
 */
template<typename Ty>
class Sington {
public:
    static Ty* getInstance() {
        if (m_instance == nullptr) {
            QMutexLocker lock(&m_mutex);
            if (m_instance == nullptr) {
                m_instance = new Ty;
            }
        }
        return m_instance;
    }

    static void release() {
        if (m_instance) {
            QMutexLocker lock(&m_mutex);
            if (m_instance) {
                delete m_instance;
                m_instance = nullptr;
            }
        }
    }

    Q_DISABLE_COPY_MOVE(Sington<Ty>);
    Sington() = delete;
    ~Sington() = delete;

private:
    static Ty* m_instance;
    static QMutex m_mutex;
};

/* 静态成员需要显示在外部进行定义 */
template<typename Ty>
Ty* Sington<Ty>::m_instance = nullptr;

template<typename Ty>
QMutex Sington<Ty>::m_mutex;

#endif // SINGTON_H
